package com.gdcp.rv;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

import static android.support.v4.app.ActivityCompat.startActivityForResult;

public class Myadapter extends RecyclerView.Adapter<Student_holder> {
        private ArrayList<Student> arrayList=new ArrayList<>();
        private Context context;
        private IOnDelListener delListener;

        public void changeData(ArrayList<Student> stu){

            this.arrayList=stu;
            notifyDataSetChanged();

        }

    public Myadapter(ArrayList<Student> arrayList, Context context,IOnDelListener delListener) {
        this.delListener=delListener;
        this.arrayList = arrayList;
        this.context = context;
    }

    @NonNull
    @Override
    public Student_holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                     View view= LayoutInflater.from(context).inflate(R.layout.list_rev,parent,false);
                     Student_holder student_holder=new Student_holder(view);
                                    return student_holder;

    }

    @Override
    public void onBindViewHolder(@NonNull Student_holder holder, final int position) {
              Student s=arrayList.get(position);
              holder.name.setText(s.getName());
              holder.age.setText(String.valueOf(s.getAge()));
              holder.image.setImageResource(s.getImage());
              //单击删除
        holder.del_stu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                         delListener.del(position);
            }
        });
        //单击每一项；
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  Intent intent = new Intent(context,EditActivity.class);
                  intent.putExtra("NAME",arrayList.get(position).getName());
                  intent.putExtra("AGE",arrayList.get(position).getAge());
                  intent.putExtra("ID",arrayList.get(position).getImage());
                  intent.putExtra("NO",arrayList.get(position).getNo());
                 ((MainActivity)context).startActivityForResult(intent,2011);

            }
        });


    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }
}
